package arrays;

/**
 * @author pengfei.hpf
 * @date 2020/3/20
 * @verdion 1.0.0
 * 给定一个整数数组，判断数组中是否有两个不同的索引 i 和 j，使得 nums [i] 和 nums [j] 的差的绝对值最大为 t，并且 i 和 j 之间的差的绝对值最大为 ķ。
 *
 * 示例 1:
 *
 * 输入: nums = [1,2,3,1], k = 3, t = 0
 * 输出: true
 * 示例 2:
 *
 * 输入: nums = [1,0,1,1], k = 1, t = 2
 * 输出: true
 * 示例 3:
 *
 * 输入: nums = [1,5,9,1,5,9], k = 2, t = 3
 * 输出: false
 */
public class ContainsNearbyAlmostDuplicate {
    public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
        if(nums == null || k < 1){
            return false;
        }
        int left = 0;
        int right = 1;
        long com = (long) t;
        while(left < nums.length){
            right = left + 1;
            while(right - left <= k && right < nums.length){
                long sub = (long)nums[right] - (long)nums[left];
                if(Math.abs(sub) <= com){
                    return true;
                }
                right ++;
            }
            left ++;
        }
        return false;
    }
}
